package com.ge.med.cchelper.popup.job;

import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

import com.ge.med.cchelper.Constants;
import com.ge.med.cchelper.bean.Response;
import com.ge.med.cchelper.util.GZipHelper;
import com.ge.med.cchelper.util.LogUtil;
import com.ge.med.cchelper.util.SSLhelper;
import com.ge.med.cchelper.util.ftp.FTPFactory;
import com.ge.med.cchelper.util.ftp.FtpHelper;

public class SyncAllJob extends BaseJob {

	private IProject selectedIProject;

	private String gzipFileName;
	private String deleteDir;
	private String findCmd;
	private String tarCmd;
	private String gzipRemotePath;
	private String savedDownloadDir;

	public SyncAllJob(String message, IProject selectedIProject,
			String savedDownloadDir, String regex) {
		super(message);
		this.selectedIProject = selectedIProject;

		this.savedDownloadDir = savedDownloadDir;
		
		String vobsPath = csession.getVobRoot();
		String middleDir = savedDownloadDir.substring(vobsPath.length());

		gzipFileName = middleDir.replaceAll("/{1,}", "_") + ".gz";
		gzipRemotePath = sourceTempDir + "/" + gzipFileName;

		deleteDir = sourceTempDir + "/" + middleDir;
		// findCmd = "find . -regex \\\".*\\.\\(cxx\\|c\\|h\\)\\\" | cpio -pdm "
		// +sourceTempDir+"/"+ middleDir;
		findCmd = "cd " + savedDownloadDir +";/usr/bin/find . -regex '" + regex
				+ "' | cpio -pdmu " + sourceTempDir + "/" + middleDir;
		tarCmd = "tar -zcf " + gzipRemotePath + " "+Constants.VOB+"/" + middleDir;
	}

	protected IStatus start(IProgressMonitor arg0) {

		SSLhelper sslHelper = new SSLhelper(csession);
		
		Response res0 = sslHelper.executeCT("cd "+savedDownloadDir);
		if (res0.getStatus() != 0) {
			return Status.CANCEL_STATUS;
		}
		
		Response res = sslHelper.executeCT(findCmd);
		//int flag = res.getStatus();
		Response res1 = sslHelper.executeCT("cd " + root + ";" + tarCmd);
		if (res1.getStatus() == 0) {			
			FtpHelper ftp = null;
			try {
				ftp = FTPFactory.getConnectionInstance(csession.getHost(), ".");
				String dirtemp = selectedIProject.getLocation().toOSString();
				if (ftp.downloadPlus(gzipRemotePath, dirtemp + File.separator
						+ gzipFileName,arg0,false)) {
					try {
						GZipHelper gzip = new GZipHelper(dirtemp
								+ File.separator + gzipFileName);
						gzip.unTargzFile(dirtemp);

						try {
							selectedIProject.refreshLocal(
									IProject.DEPTH_INFINITE, null);
						} catch (CoreException e1) {
							e1.printStackTrace();
							LogUtil.err("refresh failed." + e1.getMessage());
						}
					} catch (Exception e) {
						LogUtil.err("unpack failed." + e.getMessage());
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				LogUtil.err("Ftp failed." + e.getMessage());
			} finally {
				try {
					ftp.closeServer();
				} catch (Exception e) {
					LogUtil.err("Colse FTP server failed." + e.getMessage());
				}
			}
			new DeleteTempJob(gzipRemotePath,true).schedule();	
		}
		new DeleteTempJob(deleteDir).schedule();		
		return Status.OK_STATUS;

	}

}
